﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using System.Linq.Dynamic.Core;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using KDS.Mes.Authorization;
using KDS.Mes.Dto;
using KDS.Mes.Mes.Alcs.Dtos;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using KDS.Mes.Mes.Cartypes;

namespace KDS.Mes.Mes.Alcs
{
    /// <summary>
    /// ALC信息服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Bom_Alcs)]
    public class AlcAppService : MesAppServiceBase, IAlcAppService
    {
        private readonly IRepository<Alc, int> _alcRepository;
        private readonly IAlcListExcelExporter _alcListExcelExporter;
        private readonly AlcManage _alcManage;
        private readonly IRepository<Cartype, int> _cartypeRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        public AlcAppService(
            IRepository<Alc, int> alcRepository,
            AlcManage alcManage, 
            IAlcListExcelExporter alcListExcelExporter,
            IRepository<Cartype, int> cartypeRepository
          )
        {
            _alcRepository = alcRepository;
            _alcManage = alcManage;
            _alcListExcelExporter = alcListExcelExporter;
            _cartypeRepository = cartypeRepository;
        }

        #region ALC信息管理

        public PagedResultDto<AlcListDto> GetPagedAlcs(GetAlcInput input)
        {
            //初步过滤
            var query = _alcRepository.GetAll();
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();

            return new PagedResultDto<AlcListDto>(tasksCount, taskList.MapTo<List<AlcListDto>>());
        }


        /// <summary>
        /// 根据查询条件获取ALC信息分页列表
        /// </summary>
        public async Task<PagedResultDto<AlcListDto>> GetPagedAlcsAsync(GetAlcInput input)
        {
            var query = _alcRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var alcCount = await query.CountAsync();

            var alcs = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var alcListDtos = alcs.MapTo<List<AlcListDto>>();
            return new PagedResultDto<AlcListDto>(
            alcCount,
            alcListDtos
            );
        }

        /// <summary>
        /// 通过Id获取ALC信息信息进行编辑或修改
        /// </summary>
        public async Task<GetAlcForEditOutput> GetAlcForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetAlcForEditOutput();

            AlcEditDto alcEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _alcRepository.GetAsync(input.Id.Value);
                alcEditDto = entity.MapTo<AlcEditDto>();
            }
            else
            {
                alcEditDto = new AlcEditDto();
            }

            output.Alc = alcEditDto;
            output.Cartypes = _cartypeRepository.GetAllList()
             .Select(c => new ComboboxItemDto(c.Id.ToString(), c.CartypeName) { IsSelected = output.Alc.CartypeId == c.Id })
             .ToList();
            return output;
        }

        /// <summary>
        /// 通过指定id获取ALC信息ListDto信息
        /// </summary>
        public async Task<AlcListDto> GetAlcByIdAsync(EntityDto<int> input)
        {
            var entity = await _alcRepository.GetAsync(input.Id);

            return entity.MapTo<AlcListDto>();
        }

        /// <summary>
        /// 新增或更改ALC信息
        /// </summary>
        public async Task CreateOrUpdateAlcAsync(CreateOrUpdateAlcInput input)
        {
            if (input.AlcEditDto.Id.HasValue)
            {
                await UpdateAlcAsync(input.AlcEditDto);
            }
            else
            {
                await CreateAlcAsync(input.AlcEditDto);
            }
        }

        /// <summary>
        /// 新增ALC信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_Alcs_Create)]
        public virtual async Task<AlcEditDto> CreateAlcAsync(AlcEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<Alc>();

            entity = await _alcRepository.InsertAsync(entity);
            return entity.MapTo<AlcEditDto>();
        }

        /// <summary>
        /// 编辑ALC信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_Alcs_Edit)]
        public virtual async Task UpdateAlcAsync(AlcEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _alcRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _alcRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除ALC信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_Cartypes_Delete)]
        public async Task DeleteAlcAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _alcRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除ALC信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_Cartypes_Delete)]
        public async Task BatchDeleteAlcAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _alcRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion ALC信息管理

        #region ALC信息的Excel导出功能

        public async Task<FileDto> GetAlcToExcel()
        {
            var entities = await _alcRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<AlcListDto>>();

            var fileDto = _alcListExcelExporter.ExportAlcToFile(dtos);

            return fileDto;
        }

        #endregion ALC信息的Excel导出功能
    }
}